'''

    The KeyLabel is a convenient label widget to
    show a key along with some state

'''

from PyQt4 import QtGui, QtCore

class KeyChangeEvent(QtCore.QEvent):
    def __init__(self, key):
        QtCore.QEvent.__init__(self, QtCore.QEvent.User+1)
        self.key = key
 
class KeyTransition(QtCore.QAbstractTransition):
    def __init__(self, key=None):
        QtCore.QAbstractTransition.__init__(self)
        self.key = key
        
    def eventTest(self, event):
        if event.type() != QtCore.QEvent.Type(QtCore.QEvent.User+1):
            return False
        return self.match(event.key)

    def onTransition(self, event):
        pass
    
    def match(self, key):
        raise NotImplementedError
    
class KeySetTransition(KeyTransition):
    def match(self, key):
        return key and True or False
    
class KeyClearTransition(KeyTransition):
    def match(self, key):
        return not key and True or False

class KeyLabel(QtGui.QLabel):
    def __init__(self, parent):
        QtGui.QLabel.__init__(self, parent)
        font = self.font()
        font.setPointSize(10)
        font.setBold(True)
        self.setFont(font)
        self.setLineWidth(0)
        self.setFrameShape(QtGui.QFrame.Panel|QtGui.QFrame.Raised)
        self.setAlignment(QtCore.Qt.AlignHCenter|QtCore.Qt.AlignVCenter)
        self.setAutoFillBackground(True)
        defaultPalette = QtGui.QPalette(self.palette)
        
        self.stateMachine = QtCore.QStateMachine(self)
        parentState = self.stateMachine
        
        self.stateKey = QtCore.QState(parentState)
#        self.stateKey.assignProperty(
#            self, "frameShape", QtGui.QFrame.WinPanel,
#        )
        self.stateKey.assignProperty(
            self, "lineWidth", 3,
        )
#        self.stateKey.assignProperty(
#            self, "alignment", QtCore.Qt.AlignLeft|QtCore.Qt.AlignVCenter,
#        )
        self.stateKey.assignProperty(
            self, "palette", QtGui.QPalette(QtGui.QColor(250,250,200)),
        )

        self.stateNoKey = QtCore.QState(parentState)
#        self.stateNoKey.assignProperty(
#            self, "frameShape", QtGui.QFrame.Panel|QtGui.QFrame.Sunken,
#        )
        self.stateNoKey.assignProperty(
            self, "lineWidth", 1,
        )
#        self.stateNoKey.assignProperty(
#            self, "alignment", QtCore.Qt.AlignRight|QtCore.Qt.AlignVCenter,
#        )
        self.stateNoKey.assignProperty(
            self, "palette", defaultPalette,
        )

        toStateNoKey = KeyClearTransition()
        toStateNoKey.setTargetState(self.stateNoKey)
        self.stateKey.addTransition(toStateNoKey)
        
        toStateKey = KeySetTransition()
        toStateKey.setTargetState(self.stateKey)
        self.stateNoKey.addTransition(toStateKey)
        
        parentState.setInitialState(self.stateKey)
        self.stateMachine.start()
        
    def setKey(self, key):
        self.setText(key or ' ')
        self.stateMachine.postEvent(KeyChangeEvent(key))
        